from rest_framework import generics, viewsets, permissions, filters, mixins, status
from .models import *
from.serializers import *
from rest_framework import filters
from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema

class ChapterListView(generics.ListAPIView):
    queryset = Chapter.objects.all()  # 获取所有章节
    serializer_class = ChapterSerializer  # 使用章节序列化器
    filter_backends = [filters.SearchFilter]  # 使用搜索过滤器
    search_fields = ['subject__name', 'name']  # 允许根据学科名称和章节名称搜索
    
    @swagger_auto_schema(
        operation_summary="获取章节列表",
        operation_description="获取所有章节信息，支持按学科名称和章节名称搜索",
        manual_parameters=[
            openapi.Parameter(
                'search', openapi.IN_QUERY,
                description="搜索关键词，用于按学科名称和章节名称搜索",
                type=openapi.TYPE_STRING
            )
        ],
        responses={
            200: openapi.Response(
                description="获取章节列表成功",
                schema=ChapterSerializer(many=True)
            )
        }
    )
    def get(self, request, *args, **kwargs):
        return super().get(request, *args, **kwargs)

# Create your views here.
